﻿using LitJson;
using LNet;
using OestsCommon.Model;
using OestsServer.Net;

namespace OestsServer.Handlers.UnityHandlers
{
    /// <summary>
    ///交易
    /// </summary>
    public class ExchangeHandler : HandlerBase<UnityClientChannel>
    {
        public override PacketKey PKey
        {
            get
            {
                return PacketKey.Exchange;
            }
        }
        public override void Execute(UnityClientChannel client, Request packet)
        {
            UnityClientChannel uc = client as UnityClientChannel;
            if (packet.RequestSubCode == SubCode.TryStartExchange)
                TryStartExchange(uc, packet);

            else if (packet.RequestSubCode == SubCode.StartExchange)
                StartExchange(uc, packet);

            else if (packet.RequestSubCode == SubCode.CancelExchange)
                CancelExchange(uc, packet);

            else if (packet.RequestSubCode == SubCode.LockedExchange)
                LockedExchange(uc, packet);

            else if (packet.RequestSubCode == SubCode.CompleteExchange)
                CompleteExchange(uc, packet);

        }

        private void TryStartExchange(UnityClientChannel client, Request packet)
        {
            //value1:交易发起方用户ID
            //value2: 接受交易方用户ID
            //向接受交易方发送交易消息：
            Struct2<int, int> data = JsonMapper.ToObject<Struct2<int, int>>(packet.DataStr);

            ResponsePacket = new Response(packet);
            //判断接收者是否存在
            bool b = client.Manager.CheckUserIsExist(client.ProjectType,/*client.User.Type, */data.Value2);
            if (b)
            {
                ResponsePacket.ReturnCode = ReturnCode.Success;
                client.Send(ResponsePacket);
                UnityClientChannel.Signal_ID.Dispatch(data.Value2, ResponsePacket);
            }
            else
            {
                ResponsePacket.ReturnCode = ReturnCode.Fail;
                ResponsePacket.RetrunMessage = "交易对象不存在";
                client.Send(ResponsePacket);
            }
        }

        private void StartExchange(UnityClientChannel client, Request packet)
        {
            //value1:交易发起方用户ID
            //value2: 接受交易方用户ID
            //向发起交易方发送交易消息：
            Struct2<int, int> data = JsonMapper.ToObject<Struct2<int, int>>(packet.DataStr);
            ResponsePacket = new Response(packet);
            //判断接收者是否存在
            bool b = client.Manager.CheckUserIsExist(client.ProjectType, data.Value2);
            if (b)
            {
                ResponsePacket.ReturnCode = ReturnCode.Success;
                client.Send(ResponsePacket);
                UnityClientChannel.Signal_ID.Dispatch(data.Value1, ResponsePacket);
            }
            else
            {
                ResponsePacket.ReturnCode = ReturnCode.Fail;
                ResponsePacket.RetrunMessage = "交易对象不存在";
                client.Send(ResponsePacket);
            }
        }
        private void CancelExchange(UnityClientChannel client, Request packet)
        {

            // value1: 发消息方用户ID
            // value2: 接收消息方用户ID
            // value3: 取消原因
            Struct3<int, int, string> data = JsonMapper.ToObject<Struct3<int, int, string>>(packet.DataStr);

            ResponsePacket = new Response(packet);

            //判断接收者是否存在
            bool b = client.Manager.CheckUserIsExist(client.ProjectType, data.Value2);
            if (b)
            {
                ResponsePacket.ReturnCode = ReturnCode.Success;
                client.Send(ResponsePacket);
                UnityClientChannel.Signal_ID.Dispatch(data.Value2, ResponsePacket);
            }
            else
            {
                ResponsePacket.ReturnCode = ReturnCode.Fail;
                ResponsePacket.RetrunMessage = "交易对象不存在";
                client.Send(ResponsePacket);
            }
        }
        private void LockedExchange(UnityClientChannel client, Request packet)
        {
            //value1:锁定物品方用户ID
            // value2: 接收锁定消息方用户ID
            //  value3:锁定物品枚举集合

            /*                      
             *  向 接收锁定消息方发送物品锁定消息：
             *  value1:锁定物品方用户ID
             *  value2:锁定物品枚举集合
             */
            Struct3<int, int, int[]> data = JsonMapper.ToObject<Struct3<int, int, int[]>>(packet.DataStr);
            ResponsePacket = new Response(packet);
            //判断接收者是否存在
            bool b = client.Manager.CheckUserIsExist(client.ProjectType, data.Value2);
            if (b)
            {
                ResponsePacket.ReturnCode = ReturnCode.Success;
                client.Send(ResponsePacket);
                UnityClientChannel.Signal_ID.Dispatch(data.Value2, ResponsePacket);
            }
            else
            {
                ResponsePacket.ReturnCode = ReturnCode.Fail;
                ResponsePacket.RetrunMessage = "交易对象不存在";
                client.Send(ResponsePacket);
            }
        }
        private void CompleteExchange(UnityClientChannel client, Request packet)
        {
            /*
             value1:交易发起方用户ID
             value2:接受交易方用户ID
             value3:锁定物品枚举集合
            */
            Struct3<int, int, int[]> data = JsonMapper.ToObject<Struct3<int, int, int[]>>(packet.DataStr);
            /*
             (需通过Value1,value2和发送消息者判断出接收消息者)向接收方发送交易完成消息：
            value1:交易发起方用户ID
            value2:接受交易方用户ID
            value3:锁定物品枚举集合
            */

            //判断接收者
            int reciveId = 0;
            if (client.User.DBid == data.Value1)
                reciveId = data.Value2;
            else
                reciveId = data.Value1;
            ResponsePacket = new Response(packet);
            //判断接收者是否存在
            bool b = client.Manager.CheckUserIsExist(client.ProjectType, data.Value2);
            if (b)
            {
                ResponsePacket.ReturnCode = ReturnCode.Success;
                client.Send(ResponsePacket);
                UnityClientChannel.Signal_ID.Dispatch(data.Value2, ResponsePacket);
            }
            else
            {
                ResponsePacket.ReturnCode = ReturnCode.Fail;
                ResponsePacket.RetrunMessage = "交易对象不存在";
                client.Send(ResponsePacket);
            }
        }
    }
}
